package com.youi.webapp;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sec.sdk.constants.SecGatewayConstants;
import com.sec.sdk.exception.SecGatewayException;
import com.sec.sdk.utils.SecSignature;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.entity.Leaderwithdraw;
import com.youi.entity.Sharecloudrecord;
import com.youi.entity.Tremittance;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.service.LeaderwithdrawService;
import com.youi.service.MerchantService;
import com.youi.service.MerchantwithdrawService;
import com.youi.service.MerchantwithdrawdetailService;
import com.youi.service.SharecloudrecordService;
import com.youi.service.TremittanceService;
import com.youi.service.UserorderService;
import com.youi.service.YibaodivideService;
import com.youi.service.YibaopaymentService;
import com.youi.third.shareclound.SharecloundUtil;
import com.youi.third.yibaopay.YibaopayUtil;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JsonUtil;
import com.youi.util.LogUtil;

/**
 * 易宝回调控制层
 * @author Administrator
 *
 */
@Controller
public class CallbackController extends HttpRequestManage {

	@Autowired
	private TremittanceService tremittanceService;
	@Autowired
	private MerchantwithdrawService merchantwithdrawService;
	@Autowired
	private MerchantwithdrawdetailService merchantwithdrawdetailService;
	@Autowired
	private UserorderService userorderService;
	@Autowired
	private YibaopaymentService yibaopaymentService;
	@Autowired
	private MerchantService merchantService;
	@Autowired
	private YibaodivideService yibaodivideService;
	@Autowired
	private SharecloudrecordService sharecloudrecordService;
	@Autowired
	private LeaderwithdrawService leaderwithdrawService;
	
	
	/**
	 * 云享打款回调
	 * @param modelMap
	 * @param request
	 * @return
	 * @throws BusinessException
	 * @throws SecGatewayException 
	 */
	@ResponseBody
	@RequestMapping("checkShareclound")
	public String checkShareclound(ModelMap modelMap,HttpServletRequest request) throws BusinessException, SecGatewayException{
		LogUtil.Info("云享打款回调");
		String method=getString(SecGatewayConstants.METHOD, request);
		String sign_type=getString(SecGatewayConstants.SIGN_TYPE, request);
		String sign=getString(SecGatewayConstants.SIGN, request);
		String timestamp=getString(SecGatewayConstants.TIMESTAMP, request);
		String version=getString(SecGatewayConstants.VERSION, request);
		String merchant_request_no=getString(SecGatewayConstants.MERCHANT_REQUEST_NO, request);
		String biz_content_key=getString(SecGatewayConstants.BIZ_CONTENT_KEY, request);
		boolean flag=SharecloundUtil.sharecloundCallback(method, sign_type, sign, timestamp, version, merchant_request_no, biz_content_key);
		if (!flag) {
			LogUtil.Info("================回调验签失败");
			return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "回调验签失败");
		}
		JSONObject data=JSONObject.parseObject(biz_content_key);
		if (data==null) {
			LogUtil.Info("========回调数据获取失败");
			return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "回调数据获取失败");
		}
		Map<String, Object> prams=new HashMap<String, Object>();
		prams.put(Common.DELSTATUS, DelStatus.NODEL);
		prams.put("custBatchNo", data.getString("custBatchNo"));
		Sharecloudrecord sharecloudrecord=sharecloudrecordService.getByParam(prams);
		if (sharecloudrecord==null) {
			LogUtil.Info("========云享流水信息不存在");
			return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "云享流水信息不存在");
		}
		prams=new HashMap<String, Object>();
		prams.put(Common.DELSTATUS, DelStatus.NODEL);
		prams.put("withdrawNo", sharecloudrecord.getOrderNo());
		Tremittance tremittance=tremittanceService.getByParam(prams);
		if (tremittance==null) {
			LogUtil.Info("========申请打款单不存在");
			return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "申请打款单不存在");
		}
		prams=new HashMap<String, Object>();
		prams.put(Common.DELSTATUS, DelStatus.NODEL);
		prams.put("orderNo", sharecloudrecord.getOrderNo());
		Leaderwithdraw leaderwithdraw=leaderwithdrawService.getByParam(prams);
		if (leaderwithdraw==null) {
			LogUtil.Info("========提现订单不存在");
			return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "提现订单不存在");
		}
		if (data.getInteger("batchStatus")==1) {	
			tremittance.setPayStatus(2);
			tremittance.setGmtReality(new Date());
			leaderwithdraw.setGmtWithdraw(new Date());
			leaderwithdraw.setStatus(2);
		}else {
			tremittance.setPayStatus(-1);
			leaderwithdraw.setStatus(3);
		}
		
		leaderwithdrawService.saveOrUpdate(leaderwithdraw);
		tremittanceService.saveOrUpdate(tremittance);
		return "SUCCESS";
	}
	
	
}
